package com.wolfword.crawler.server.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.wolfword.common.domain.Result;
import com.wolfword.common.domain.ResultConstant;
import com.wolfword.crawler.server.config.Config;
import com.wolfword.crawler.server.entity.CrawlTaskEntity;
import com.wolfword.crawler.server.service.CrawlTaskService;

/**
 * 任务统计
 * @author ServerZhang
 * @date 2018年1月24日
 */
@RestController
@RequestMapping("/statistic")
@Api(value = "/statistic", description = "任务统计分析")
public class TaskStatisticController {
	
	private final Logger logger = LoggerFactory.getLogger(TaskStatisticController.class);
	
	@Autowired
	private Config config;
	
    @Autowired
    private CrawlTaskService crawlTaskService;
    
    /**
	 * 统计分析列表
     * @param request
     * @param url
     * @param pageIndex
     * @param pageSize
     * @return
     * @author ServerZhang
     * @date 2018年1月17日
     */
    @ApiOperation("统计分析列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "url", value = "地址中包含的串", paramType = "query", dataType = "string", required = false),
            @ApiImplicitParam(name = "sortType", value = "排序：0默认，1邮箱，2电话，3职称，4子页面邮箱，5子页面电话，6子页面职称", paramType = "query", dataType = "Integer", required = false),
            @ApiImplicitParam(name = "pageIndex", value = "页码", paramType = "query", dataType = "Integer", required = false),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", paramType = "query", dataType = "Integer", required = false)
    })
	@PostMapping(value="/list")
	public Result list(HttpServletRequest request,
			String url, @RequestParam(value="sortType",defaultValue="0")Integer sortType,
			@RequestParam(value="pageIndex",defaultValue="0")Integer pageIndex,
			@RequestParam(value="pageSize",defaultValue="50") Integer pageSize){
    	
    	List<CrawlTaskEntity> list = null;
    	Result result = null;
		try {
			list = crawlTaskService.statistic(url, sortType, pageIndex, pageSize);
			result = new Result(ResultConstant.SUCCESS,new PageInfo<>(list));
		} catch (Exception e) {
			logger.info(e.getMessage());
			e.printStackTrace();
			result = new Result(ResultConstant.GENERAL_ERROR.setMessage(e.getMessage()));
		}
		
	    return result;
	} 
    
}
